<?php
/* 
 *销售详情
 */
class Wholesale_detailAction extends TransDetailAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            $map['name'] = array('like',"%".$key."%");//产品名称
            $map['wholesale_no'] = array('like',"%".$key."%");//销售单号
            $map['logino'] = array('like',"%".$key."%");
            /* exact filter
            $map['Warehouse.name'] = array('like',"%".$key."%");//仓库
            $map['Customer.name'] = array('like',"%".$key."%");//customer
            $map['number'] = array('eq',$key);
            //$map['minstock'] = array('like',"%".$key."%");*/
            $map['_logic'] = "or";
        }
        if(!empty($_REQUEST['sale_id'])){
           $map['wholesale_id'] = $_REQUEST['sale_id'];
        }
        if(!empty($_REQUEST['user_id'])){
           $map['user_id'] = $_REQUEST['user_id'];
        }
    }

    public function _before_index(){
        parent::_before_index();
        $this->assign('customers',getModelField('Customer','id,name','sort'));
        $this->assign('users',getModelField('User'));
    }
    public function  index() {
        parent::index('SaleView');    
    }

    public function  _before_insert() {
        $pid = $_POST['product_id'];
         if(!$pid)
            $this->error('请选择至少一个产品');
        if($pid != array_unique($pid))
          $this->error('您选择的产品存在重复，请去重后再操作！');

          $number = $_POST['number'];
          foreach ($number as $key => $value) {
            if($value <1 )
              $this->error("产品数量必须大于等于 1 ");
            continue;
          }

        $name=$this->getActionName();
        $parent_name = strtolower( str_replace('_detail', '', $name) );
        if(empty($_POST[$parent_name.'_id']) )
            $this->error("必须选择单号");
    }

   // $no_reutrn 默认为0，则返回操作提示，否则不返回
    //is_gift 是否是赠品。默认为0，非赠品。1为赠品
    public function insert($no_return = 0,$is_gift = 0){
        $data = $_POST;
        $sale = M('Wholesale')->getById($_POST['wholesale_id'] );

        $small_count = count($data['product_id']);
        $m = 'wholesale';
        $sale_detail = M('Wholesale_detail');

        for( $i =0 ; $i<$small_count;$i++) {
           $detail_data= array(
                   $m.'_id'=> $data[$m.'_id'],
                   $m.'_no'=> $data[$m.'_no'],
                   'product_id'=> $data['product_id'][$i],
                   'lot'=> $data['lot'][$i],   //产品批号
                   'exp'=> $data['exp'][$i],   //截止有效期
                   'price'=> $data['price'][$i],
                   'number'=> $data['number'][$i],
                   'customer_id'=> $data['customer_id'],    //冗余设计
                   'is_gift' => $is_gift,
                   'create_time'=> time(),
           );
           $list = $sale_detail->add($detail_data);
           //TODO 以下部分做成公共的模块
           $warehouse_id = $sale['warehouse_id'];
           //A('Trans')->updateStock($detail_data['product_id'], $warehouse_id, 'out', $detail_data['number'] );
           //如果修改了价格，则同时更新财务记录
            updateTotalPrice('Wholesale',$list,$detail_data['number'],$detail_data['price']);

            //update sum_number
            updateSumNumber('Wholesale',$list,$detail_data['number']);

            if($no_return == 0){ //自动增加的，则不记录日志
              /******************日志记录*********************/
               $mod_field = getModelName ($list, "Wholesale_detail",$field);
              //如果未取到字段值 ，则返回对应的ID编号
              $msg = '新增 Wholesale_detail: '.$mod_field.' (编号：'.$list.')';
              A('Action')->writeLog($msg,'新增','Wholesale_detail',$list);
            }
        }
        if($no_return == 0){
            $jumpUrl = $_REQUEST['jumpUrl'];
            if(empty($jumpUrl))
                $jumpUrl = Cookie::get ( '_currentUrl_' );
            $this->assign ( 'jumpUrl', $jumpUrl );
            $this->success ('新增成功!');
        }
            
                       
    }



 

    //TODO 通过订单详情ID返回新的可复制的数据，ajax返回
    public function getSaleDetail()
    {
      $sale_detail_id = $_POST['sale_detail_id'];
      $detail = M('Wholesale_detail')->getById($sale_detail_id);
      if(is_array($detail)){
        $tr_str = '<tr><form action="'.__APP__.'/Wholesale_detail/copy_detail" method="post">';
          $tr_str .= '<td>-- <input type="hidden" name="wholesale_id" value="'.$detail['wholesale_id'].'">
          <input type="hidden" name="wholesale_no" value="'.$detail['wholesale_no'].'">
          <input type="hidden" name="pid" value="'.$detail['product_id'].'">
           </td>';
          $tr_str .= "<td></td>"; //规格
          $tr_str .= "<td>--</td>";  //商品编号
          $tr_str .= "<td>--</td>"; //商品名称
          $tr_str .= "<td>--</td>";   //库位
          $tr_str .= "<td><input type='text' name='price[]' class='span1' value='".$detail['price']."'></td>";   //价格
          $tr_str .= "<td><input type='text' name='number[]' class='span1' value='".$detail['number']."'></td>";  //商品编号 //数量
          $tr_str .= "<td>".getProductUnit($detail['product_id'])."</td>";   //单位
          $tr_str .= "<td>--</td>";   //小计
          $tr_str .= "<td>--</td>";   //库存
          $tr_str .= "<td><input type='hidden' name='jumpUrl' value='".__APP__."/Wholesale/edit/id/".$detail['sale_id']."'><input type='submit' value='新增' class='btn'> </td>";
          $tr_str .=" <td class='span1'><a href='javascript:void(0)' class='btn' alt='删除' onclick='removerow(this);'>删除</a></td>";
          $tr_str .="<td></td></form></tr>";

          $data['sale_detail'] = $tr_str ;
          $this->ajaxReturn($data, '', 1);
      }else{
        $this->ajaxReturn(null, '订单商品明细无法获取！', 0);
      }
      
    }

  



}
?>
